package com.ktwlrj.core.base;

import com.ktwlrj.core.constant.ResultCode;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * MVC控制器基类
 *
 * @author Administrator
 */
@ApiResponses({
        @ApiResponse(code = 401, message = "未授权"),
        @ApiResponse(code = 200, message = "成功"),
        @ApiResponse(code = 403, message = "拒绝访问"),
        @ApiResponse(code = 404, message = "请求路径不存在"),
        @ApiResponse(code = 500, message = "服务器内部错误")
})
public class SuperController {
    protected HttpServletRequest request;
    protected HttpSession session;


    @Autowired
    public void setSuperController(HttpServletRequest request, HttpSession session) {
        this.session = session;
        this.request = request;
    }


    /**
     * 成功响应输出
     *
     * @param model ${@link T} 响应实体
     * @return ActionResult<T> ${@link ActionResult<T>}
     * @author zhouxuanhong
     * @date 2019-05-15 14:33
     */
    public <T> ActionResult<T> success(T model,String message) {
        ActionResult<T> response = new ActionResult<>();
        response.setCode(ResultCode.SUCCESS.getValue());
        response.setSuccess(true);
        response.setData(model);
        response.setMessage(message);
        return response;
    }


    /**
     * 成功响应输出
     *
     * @param model ${@link String} 响应实体
     * @return ActionResult<T> ${@link ActionResult<T>}
     * @author zhouxuanhong
     * @date 2019-05-15 14:34
     */
    public <T> ActionResult<T> success(T model) {
        ActionResult<T> response = new ActionResult<>();
        response.setCode(ResultCode.SUCCESS.getValue());
        response.setSuccess(true);
        response.setData(model);
        return response;
    }


    /**
     * 错误响应输入
     *
     * @param resultCode ${@link ResultCode} 响应错误枚举
     * @param model      ${@link T} 响应错误实体信息
     * @return ActionResult<T> ${@link ActionResult<T>}
     * @author zhouxuanhong
     * @date 2019-05-15 14:35
     */
    public <T> ActionResult<T> error(ResultCode resultCode, T model) {
        ActionResult<T> response = new ActionResult<>();
        response.setSuccess(false);
        response.setMessage(resultCode.getDescription());
        response.setCode(resultCode.getValue());
        response.setData(model);
        return response;
    }


    /**
     * 响应错误枚举
     *
     * @param resultCode ${@link ResultCode} 响应错误枚举
     * @return ActionResult<Object> ${@link ActionResult<Object>}
     * @author zhouxuanhong
     * @date 2019-05-15 14:36
     */
    public ActionResult<Object> error(ResultCode resultCode) {
        return error(resultCode, null);
    }


    /**
     * 错误响应输出
     *
     * @param code ${@link Integer} 错误代码
     * @return ActionResult<Object> ${@link ActionResult<Object>}
     * @author zhouxuanhong
     * @date 2019-05-15 14:36
     */
    public ActionResult<Object> error(Integer code) {
        return error(code, null, null);
    }


    /**
     * 错误响应输出
     *
     * @param code    ${@link Integer} 错误代码
     * @param message ${@link String} 错误提示消息
     * @return ActionResult<Object> ${@link ActionResult<Object>}
     * @author zhouxuanhong
     * @date 2019-05-15 14:38
     */
    public <T> ActionResult<T> error(Integer code, String message) {
        return error(code, message, null);
    }


    /**
     * 错误响应输出
     *
     * @param code    ${@link Integer} 错误代码
     * @param message ${@link String} 错误提示消息
     * @param model   ${@link T} 响应错误实体信息
     * @return ActionResult<T> ${@link ActionResult<T>}
     * @author zhouxuanhong
     * @date 2019-05-15 14:38
     */
    public <T> ActionResult<T> error(Integer code, String message, T model) {
        ActionResult<T> response = new ActionResult<>();
        response.setSuccess(false);
        response.setMessage(message);
        response.setCode(code);
        response.setData(model);
        return response;
    }

}
